package controller

import (
	"context"
	v1 "tFilm4/api/v1"
	"tFilm4/internal/model"
	"tFilm4/internal/service"
)

type cUser struct{}

var User = cUser{}

// SignUp 注册
func (c *cUser) SignUp(ctx context.Context, req *v1.SignUpReq) (res *v1.SignUpRes, err error) {
	_, err = c.CheckUsername(ctx, &v1.CheckUsernameReq{
		Username: req.Username,
	})
	if err != nil {
		return
	}
	err = service.User().SignUp(ctx, model.SignUpInput{
		Username: req.Username,
		Password: req.Password,
		Email:    req.Email,
	})
	return
}

// CheckUsername  检查用户可用性
func (c *cUser) CheckUsername(ctx context.Context, req *v1.CheckUsernameReq) (res *v1.CheckUsernameRes, err error) {
	err = service.User().CheckUsername(ctx, req.Username)
	return
}

// QueryUserById  根据Id查询用户
func (c *cUser) QueryUserById(ctx context.Context, req *v1.QueryUserByIdReq) (res *v1.QueryUserByIdRes, err error) {
	userInfo, err := service.User().QueryUserById(ctx, req.Id)
	if err != nil {
		return
	}
	res = &v1.QueryUserByIdRes{UserInfo: userInfo}
	return
}

// SignIn 用户登录
func (c *cUser) SignIn(ctx context.Context, req *v1.SignInReq) (res *v1.SignInRes, err error) {
	userInfo, err := service.User().SignIn(ctx, req.Username, req.Password)
	if err != nil {
		return
	}
	res = &v1.SignInRes{UserInfo: userInfo}
	return
}

func (c *cUser) GetUserList(ctx context.Context, req *v1.GetUserListReq) (res *v1.GetUserListRes, err error) {
	count, list, err := service.User().GetUserList(ctx, model.GetUserListInput{
		PageSize:  req.PageSize,
		PageNum:   req.PageNum,
		SearchKey: req.SearchKey,
	})
	res = &v1.GetUserListRes{UserInfos: list,
		UserCount: count}
	return
}
func (c *cUser) Update(ctx context.Context, req *v1.UpdateUserReq) (res *v1.UpdateUserRes, err error) {
	err = service.User().Update(ctx, model.UpdateInput{
		UserId:   req.UserId,
		Username: req.Username,
	})
	return
}
